<?php

namespace App\Admin\Controllers;

use App\Services\CustomerService;
use Slowlyo\OwlAdmin\Controllers\AdminController;

/**
 * 客户IP管理
 *
 * @property CustomerService $service
 */
class CustomerController extends AdminController
{
	protected string $serviceName = CustomerService::class;

	public function list()
	{
		$crud = $this->baseCRUD()
			->filterTogglable(false)
			->headerToolbar([
				$this->createButton('dialog'),
				...$this->baseHeaderToolBar()
			])
			->columns([
				amis()->TableColumn('id', 'ID')->sortable(),
				amis()->TableColumn('fcustomer.short_name',  '客户简称'),
				amis()->TableColumn('idcnode.idc_name',  '所在机房'),
				amis()->TableColumn('customer_bus.name',  '业务名称'),
				amis()->TableColumn('ip_address', 'IP地址段'),
				amis()->TableColumn('start_ip_address', '开始IP'),
				amis()->TableColumn('end_ip_address', '结束IP'),
				amis()->TableColumn('company.company_name', '签约公司'),
				amis()->TableColumn('operator.name', '操作人'),
				amis()->TableColumn('created_at', admin_trans('admin.created_at'))->type('datetime')->sortable(),
				$this->rowActions('dialog')
			]);

		return $this->baseList($crud);
	}

	public function form($isEdit = false)
	{
		return $this->baseForm()->body([

			amis()->SelectControl('customer_id', '客户简称')
				->searchable(true)
				->source('/fcustomer?_action=getData')
				->labelField('short_name')
				->valueField('id'),
			amis()->SelectControl('customer_name', '业务名称')
				->searchable(true)
				->source('/customer_bus/?fcustomer_id=${customer_id}&_action=getData')
				->labelField('name')
				->valueField('id')
				->autoFill([
					'idcnode_id' => '${idcnode_id}'
				]),
			
			// 自动填充 下面的静态框
			amis()->SelectControl('fip_address', 'IP归属段')
					->searchable(true)
					->source('/ip_addresses/?idcnode_id=${idcnode_id}&_action=getData')
					->labelField('addressBit')
					->valueField('id')
					->autoFill([
						'idcnode_name' => '${idcnode.idc_name}',
						'idcnode_id' => '${idcnode_id}'
					]),
			amis()->GroupControl()->body([
				amis()->TextControl('idcnode_name', 'IDC机房名称')
					->static()
					->id('idcnode_name'),
				amis()->TextControl('idcnode_id', 'IDC机房ID')
					->static()
					->id('idcnode_id')
			]),
				
			amis()->TextControl('ip_address', 'IP地址段'),
			amis()->GroupControl()->body([	
				amis()->TextControl('start_ip_address', '开始IP'),
				amis()->TextControl('end_ip_address', '结束IP'),
			]),
			
			
		]);
	}

	public function detail()
	{
		return $this->baseDetail()->body([
			amis()->TextControl('id', 'ID')->static(),
			amis()->TextControl('customer_name', '客户名称')->static(),
			amis()->TextControl('ip_address', 'IP地址')->static(),
			amis()->GroupControl()->body([
				amis()->TextControl('start_ip_address', '开始IP')->static(),
				amis()->TextControl('end_ip_address', '结束IP')->static(),
			]),
			amis()->TextControl('customer_address', '联系地址')->static(),
			amis()->TextControl('cutover_email', '割接邮箱')->static(),
			
			amis()->TextControl('ops_email', '运维邮箱')->static(),
			amis()->TextControl('company.company_name', '签约公司')->static(),
			amis()->TextControl('violation_email', '违规邮箱')->static(),
			amis()->TextControl('created_at', admin_trans('admin.created_at'))->static(),
			amis()->TextControl('operator.name', '操作人')->static(),
			amis()->TextControl('updated_at', admin_trans('admin.updated_at'))->static(),
		
		]);
	}
	
	public function getData()
	{	
		$fcustomer_id = request()->input('fcustomer_id');
		return $this->service->getData($fcustomer_id);
	}
}